package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.pojo.Employee;
import com.itheima.reggie.service.EmployeeService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;


    /**
     * 员工登录
     * @param employee
     * @param request
     * @return
     */
    @PostMapping("/login")
    public R<Employee> login(@RequestBody Employee employee, HttpServletRequest request){
        //先获取名字
        String username = employee.getUsername();
        //判断条件
        QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        //调用方法,获取数据库对象
        Employee emp = employeeService.getOne(queryWrapper);

        //1判断用户名是否相同
        if (!emp.getUsername().equals(username)){
            //用户名不存在
            return R.error("用户名不存在");
        }
        //2判断密码
        //用户输入的密码
        String pw1 = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());
        //数据库存在的密码
        String pw2 = emp.getPassword();
        if (!pw1.equals(pw2)){
            //密码错误
            return R.error("密码错误");
        }

        //3.判断状态
        if (emp.getStatus()==0){
            //该员工账号已被锁定
            return R.error("该员工账号已被禁用");
        }
        //登录成功,并将员工id存入到session中
        request.getSession().setAttribute("employee",emp.getId());
        return R.success(emp);
    }

    /**
     *退出登录
     */
    @PostMapping("logout")
    public R<String> logout(HttpSession session){
        //销毁session
        session.invalidate();
        return R.success("退出登录");
    }

    /**
     * 添加员工
     * @param employee
     * @return
     */
    @PostMapping
    public R<String> add(@RequestBody Employee employee,HttpServletRequest request){

        //添加其他信息

        //密码加密
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        /*//时间
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());

        //获取用户Id
        Long empId = (Long) request.getSession().getAttribute("employee");
        employee.setCreateUser(empId);
        employee.setUpdateUser(empId);*/

        employeeService.save(employee);
        return R.success("添加成功");
    }

    /**
     * 分页查询
     * @param name
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page<Employee>> selectByPage(String name, Integer page, Integer pageSize){
        //准备分页条件
        Page<Employee> pageInfo = new Page<>(page,pageSize);
        //准备查询条件
        QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(name),"name",name);
        queryWrapper.orderByDesc("update_time");
        //执行查询
        employeeService.page(pageInfo,queryWrapper);
        return  R.success(pageInfo);
    }

    /**
     * 修改员工启用/禁用状态
     * @param employee
     * @param request
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody Employee employee,HttpServletRequest request){
        /*//添加修改时间
        employee.setUpdateTime(LocalDateTime.now());
        //添加修改用户
        Long empId = (Long) request.getSession().getAttribute("employee");
        employee.setUpdateUser(empId);*/

        //调用方法执行
        employeeService.updateById(employee);
        return R.success("修改成功");
    }

    /**
     * 根据id修改员工信息
     * @param
     * @return
     */
    @GetMapping("/{id}")
    public R<Employee> updateById(@PathVariable("id") Long id){
        Employee emp = employeeService.getById(id);
        return R.success(emp);
    }

}
